<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com
 * =========================================================
 */

namespace app\shop\controller;

use app\model\community\Leader as LeaderModel;
use app\model\goods\Goods as GoodsModel;
use app\model\member\Member as MemberModel;
use app\model\system\Stat;
use Carbon\Carbon;
use app\model\web\Account as AccountModel;
use app\model\order\OrderCommon;
use app\model\order\OrderRefund as OrderRefundModel;
use app\cron\controller\Task;

class Index extends BaseShop
{
    public function add()
    {
        $task_model = new Task();
        $task_model->execute();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $this->assign('shop_status', 1);
        return $this->fetch("index/index");
    }


    public function getNumData()
    {
        $goods_model = new GoodsModel();
        //订单数
        $order = new OrderCommon();
        $waitpay = $order->getOrderCount([ [ 'order_status', '=', 0 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
        $waitsend = $order->getOrderCount([ [ 'order_status', '=', 1 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);

        $order_refund_model = new OrderRefundModel();
        $refund_num = $order_refund_model->getRefundOrderGoodsCount([
            [ "site_id", "=", $this->site_id ],
            [ "refund_status", "not in", [ 0, 3 ] ]
        ]);
        $goods_stock_alarm = $goods_model->getGoodsStockAlarm($this->site_id);

        $num_data = [
            'waitpay' => $waitpay[ 'data' ],
            'waitsend' => $waitsend[ 'data' ],
            'refund' => $refund_num[ 'data' ],
            'goods_stock_alarm' => $goods_stock_alarm[ 'data' ]
        ];

        // 团长申请
        $leader_model = new LeaderModel();
        $num_data[ 'leader_apply_count' ] = $leader_model->getLeaderCount([
            [ 'site_id', '=', $this->site_id ],
            [ 'status', 'in', LeaderModel::STATUS_AUDIT_WAIT . ',' . LeaderModel::STATUS_AUDIT_REFUSE ]
        ])[ 'data' ];

        // 会员账户
        $account_model = new AccountModel();
        $member_account = $account_model->getMemberBalanceSum($this->site_id)[ 'data' ];
        $num_data[ 'balance_withdraw_apply' ] = $member_account['balance_withdraw_apply'] ?? 0.00;
        return success(0, '', $num_data);
    }

    public function getTodayData()
    {
        $stat_shop_model = new Stat();
        $today = Carbon::now();
        $yesterday = Carbon::yesterday();
        $stat_today = $stat_shop_model->getStatShop($this->site_id, $today->year, $today->month, $today->day);
        $stat_yesterday = $stat_shop_model->getStatShop($this->site_id, $yesterday->year, $yesterday->month, $yesterday->day);
        $shop_stat_sum = $stat_shop_model->getShopStatSum($this->site_id)['data'] ?? [];
        $member_model = new MemberModel();
        $member_count = $member_model->getMemberCount([ [ 'site_id', '=', $this->site_id ] ]);
        $leader_model = new LeaderModel();
        $leader_count = $leader_model->getLeaderCount([
            [ 'site_id', '=', $this->site_id ],
            [ 'status', 'in', LeaderModel::STATUS_NORMAL . ',' . LeaderModel::STATUS_FREEZE ]
        ])[ 'data' ];
        $shop_stat_sum['member_count'] = $member_count['data'] ?? 0;
        $shop_stat_sum['leader_count'] = $leader_count['data'] ?? 0;
        //日同比
        $day_rate[ 'order_pay_count' ] = diff_rate($stat_today[ 'data' ][ 'order_pay_count' ], $stat_yesterday[ 'data' ][ 'order_pay_count' ]);
        $day_rate[ 'order_total' ] = diff_rate($stat_today[ 'data' ][ 'order_total' ], $stat_yesterday[ 'data' ][ 'order_total' ]);
        $day_rate[ 'collect_goods' ] = diff_rate($stat_today[ 'data' ][ 'collect_goods' ], $stat_yesterday[ 'data' ][ 'collect_goods' ]);
        $day_rate[ 'visit_count' ] = diff_rate($stat_today[ 'data' ][ 'visit_count' ], $stat_yesterday[ 'data' ][ 'visit_count' ]);
        $day_rate[ 'member_count' ] = diff_rate($stat_today[ 'data' ][ 'member_count' ], $stat_yesterday[ 'data' ][ 'member_count' ]);
        $day_rate[ 'leader_count' ] = diff_rate($stat_today[ 'data' ][ 'leader_count' ], $stat_yesterday[ 'data' ][ 'leader_count' ]);

        $data['day_rate'] = $day_rate;
        $data['stat_day'] = $stat_today[ 'data' ];
        $data['stat_yesterday'] = $stat_yesterday[ 'data' ];
        $data['total_data'] = $shop_stat_sum;

        return success('0', '', $data);
    }

    /**
     * 十天内数据
     */
    public function tenDayStat()
    {
        $stat_shop_model = new Stat();
        $date_day = getweeks();
        $order_total = '';
        $order_pay_count = '';
        $member_count = '';
        $leader_count = '';
        foreach ($date_day as $k => $day) {
            $dayarr = explode('-', $day);
            $stat_day[ $k ] = $stat_shop_model->getStatShop($this->site_id, $dayarr[ 0 ], $dayarr[ 1 ], $dayarr[ 2 ]);
            $order_total .= $stat_day[ $k ][ 'data' ][ 'order_total' ] . ',';
            $order_pay_count .= $stat_day[ $k ][ 'data' ][ 'order_pay_count' ] . ',';
            $member_count .= $stat_day[ $k ][ 'data' ][ 'member_count' ] . ',';
            $leader_count .= $stat_day[ $k ][ 'data' ][ 'leader_count' ] . ',';
        }
        $ten_day[ 'order_total' ] = explode(',', substr($order_total, 0, strlen($order_total) - 1));
        $ten_day[ 'order_pay_count' ] = explode(',', substr($order_pay_count, 0, strlen($order_pay_count) - 1));
        $ten_day[ 'member_count' ] = explode(',', substr($member_count, 0, strlen($member_count) - 1));
        $ten_day[ 'leader_count' ] = explode(',', substr($leader_count, 0, strlen($leader_count) - 1));

        return success('0', '', $ten_day);
    }

    /**
     * 排行榜
     */
    public function randStat()
    {
        $leader_model = new LeaderModel();
        $goods_model = new GoodsModel();
        // 排行数据
        $rank_data = [
            // 团长
            'leader' => $leader_model->getLeaderList([
                [ 'site_id', '=', $this->site_id ],
                [ 'status', 'in', LeaderModel::STATUS_NORMAL . ',' . LeaderModel::STATUS_FREEZE ],
                [ 'order_complete_money', '>', 0 ]
            ], 'cl_id, name, order_complete_money', 'order_complete_money desc, cl_id asc', '', [], '', 10)[ 'data' ],
            // 商品
            'goods' => $goods_model->getGoodsList([
                [ 'site_id', '=', $this->site_id ],
                [ 'sale_num', '>', 0 ]
            ], 'goods_id, goods_name, sale_num', 'sale_num desc, goods_id asc', 10)[ 'data' ]
        ];
        return success('0', '', $rank_data);

    }

    public function test()
    {
        //订单退款拒绝消息
        /*$message_model = new \app\model\message\Message();
        $order_goods_info = model('order_goods')->getInfo([['order_goods_id', '=', 2]]);
        $res = $message_model->sendMessage([ 'keywords' => "ORDER_REFUND_REFUSE", 'order_id' => $order_goods_info[ 'order_id' ], "order_goods_id" => $order_goods_info[ 'order_goods_id' ], "site_id" => $order_goods_info[ 'site_id' ] ]);
        dd($res);*/

        /*$res = event('OrderDeliveryWeappDelivery', ['relate_id' => 262]);
        dd($res);*/

        $res = event('MemberRechargeOrderPayWeappDelivery', ['relate_id' => 1]);
        dd($res);

        /*$event = new \addon\memberrecharge\event\MemberrechargeOrderPayNotify();
        $res = $event->handle([
            'out_trade_no' => '172377491050371061000',
            'pay_type' => 'wechatpay',
        ]);
        dd($res);*/
    }

}